package com.mooc.lib_sofa.adapter

import android.content.res.ColorStateList
import android.graphics.Color
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.button.MaterialButton
import com.mooc.ijk.widget.IJKView
import com.mooc.lib_common.model.FeedEntity
import com.mooc.lib_network.imgload.ImageLoad
import com.mooc.lib_sofa.R

/**
 * 沙发列表适配器
 * */
class SofaFeedAdapter : BaseQuickAdapter<FeedEntity,BaseViewHolder>(R.layout.layout_feed_sofa){

    var tag : String ?= null

    init {
        addChildClickViewIds(R.id.like,R.id.diss,R.id.comment,R.id.share)
    }

    override fun convert(holder: BaseViewHolder, item: FeedEntity) {
        val frame:FrameLayout=holder.getView(R.id.sofa_content_frame)
        val ijkView : IJKView = holder.getView(R.id.sofa_player_view)
        val contentImg : ImageView = holder.getView(R.id.sofa_img)
        //头像数据处理绑定
        var avatar = holder.getView<ImageView>(R.id.avatar)
        ImageLoad.loadMatch(item.author!!.avatar,avatar)
        holder.setText(R.id.author_name,item.author!!.name)
        holder.setText(R.id.create_time, TimeUtils.millis2String(item.createTime))
        //标题处理
        holder.setText(R.id.feed_title,item.feeds_text)
        //标签处理
        holder.setText(R.id.feed_tag_btn,item.activityText)
        //点赞分享等处理
        holder.setText(R.id.like,""+item.ugc!!.likeCount)
        holder.setText(R.id.diss,"踩")
        val like : MaterialButton = holder.getView(R.id.like)
        val dis : MaterialButton = holder.getView(R.id.diss)
        like.setTextColor(Color.parseColor("#3d3d3d"))
        dis.setTextColor(Color.parseColor("#3d3d3d"))
        if (item.ugc!!.hasLiked)
            like.setTextColor(Color.parseColor("#ff678f"))
        if (item.ugc!!.hasDissed)
            dis.setTextColor(Color.parseColor("#ff678f"))
        val colos = intArrayOf(
            Color.parseColor("#ff678f"),
            Color.parseColor("#3d3d3d")
        )
        //创建二维选中数组
        val state = arrayOfNulls<IntArray>(2)
        state[0]= intArrayOf(android.R.attr.state_selected)
        state[1]= intArrayOf()
        val stateList  = ColorStateList(state, colos)
        like.iconTint = stateList
        dis.iconTint = stateList
        holder.setText(R.id.comment,""+item.ugc!!.commentCount)
        holder.setText(R.id.share,""+item.ugc!!.shareCount)
        //区分内容显示
        if (item.itemType == 1){
            //图片
            frame.visibility = View.VISIBLE
            ijkView.visibility = View.GONE
            contentImg.visibility = View.VISIBLE
            ImageLoad.loadMatch(item.cover!!,contentImg)
        }else if (item.itemType == 2){
            //视频
            frame.visibility = View.VISIBLE
            ijkView.visibility = View.VISIBLE
            contentImg.visibility = View.GONE
            ijkView.setConverPath(item.cover!!)
//            ijkView.setPath(item.url!!,item.cover!!)
        }else{
            //3文字
            frame.visibility = View.GONE
        }
    }
}